package com.lyf.boot.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lyf.boot.annotation.Log;
import com.lyf.boot.core.model.*;
import com.lyf.boot.enums.BusinessType;
import com.lyf.boot.modules.system.entity.dto.SysRoleDto;
import com.lyf.boot.modules.system.entity.dto.SysRolePageDto;
import com.lyf.boot.modules.system.entity.dto.SysRolePermDto;
import com.lyf.boot.modules.system.entity.vo.SysRoleVo;
import com.lyf.boot.modules.system.service.ISysRoleService;
import com.lyf.boot.validate.group.AddGroup;
import com.lyf.boot.validate.group.EditGroup;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色接口
 * @author Luyufan
 * @date 2023/7/7 9:24
 */
@RestController
@RequestMapping("/sysRole")
@RequiredArgsConstructor
public class SysRoleController {

    private final ISysRoleService sysRoleService;

    /**
     * 角色分页
     * @param dto
     * @return
     */
    @SaCheckPermission("sys:role:list")
    @PostMapping("/getSysRolePage")
    public R<PageInfo<SysRoleVo>> getSysRolePage(@RequestBody SysRolePageDto dto){
        return R.ok(sysRoleService.getSysRolePage(dto));
    }

    /**
     * 获取所有角色
     * @return
     */
    @PostMapping("/getAllRole")
    public R<List<OptionVo>> getAllRole(){
        return R.ok(sysRoleService.getAllRole());
    }

    /**
     * 获取角色详情
     * @param dto 请求参数
     * @return 详情数据
     */
    @PostMapping("/getSysRoleInfo")
    public R<SysRoleVo> getSysRoleInfo(@RequestBody @Valid IdDto dto){
        return R.ok(sysRoleService.getSysRoleInfo(dto.getId()));
    }

    /**
     * 添加角色
     * @param dto 请求数据
     * @return 保存结果
     */
    @SaCheckPermission("sys:role:add")
    @PostMapping("/saveSysRole")
    @Log(module = "角色管理", business = "新增角色", businessType = BusinessType.INSERT)
    public R<Void> saveSysRole(@RequestBody @Validated(AddGroup.class) SysRoleDto dto){
        sysRoleService.saveSysRole(dto);
        return R.ok();
    }

    /**
     * 修改角色
     * @param dto 请求数据
     * @return 修改结果
     */
    @SaCheckPermission("sys:role:update")
    @PostMapping("/updateSysRole")
    @Log(module = "角色管理", business = "修改角色", businessType = BusinessType.UPDATE)
    public R<Void> updateSysRole(@RequestBody @Validated(EditGroup.class) SysRoleDto dto){
        sysRoleService.updateSysRole(dto);
        return R.ok();
    }

    /**
     * 删除角色
     * @param dto 请求数据
     * @return 删除结果
     */
    @SaCheckPermission("sys:role:delete")
    @PostMapping("/deleteSysRole")
    @Log(module = "角色管理", business = "删除角色", businessType = BusinessType.DELETE)
    public R<Void> deleteSysRole(@RequestBody @Valid IdArrDto dto){
        sysRoleService.deleteSysRole(dto.getIds());
        return R.ok();
    }

    /**
     * 获取角色所有权限
     * @param dto
     * @return
     */
    @PostMapping("/getSysRolePermList")
    public R<List<Long>> getSysRolePermList(@RequestBody @Valid IdDto dto){
        return R.ok(sysRoleService.getSysRolePermList(dto.getId()));
    }

    @PostMapping("/grantRolePerm")
    public R<Void> grantRolePerm(@RequestBody @Valid SysRolePermDto dto){
        sysRoleService.grantRolePerm(dto);
        return R.ok();
    }

}
